package action.article;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import service.ArticleManager;
import service.UserManager;
import service.impl.ArticleManagerImpl;
import service.impl.UserManagerImpl;

import domain.ArticleType;
import domain.User;

public class AddArticleTypeAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		ArticleType at=(ArticleType)form;
		ArticleManager am=new ArticleManagerImpl();
		ActionForward af = null; 
		// System.out.println("----" + userform.getId());		
		String action=request.getParameter("action");
		System.out.println(action);
		
		if(action.equals("editpre")){
			String id_str=request.getParameter("id");
			int id=Integer.parseInt(id_str);
			request.setAttribute("articleType", am.getArticleType(id));
			af=new ActionForward("/article/EditArticleType.jsp");
		}else if(action.equals("add")){
			System.out.println(at.getName());
			if(am.addArticleType(at))
				af=mapping.findForward("listArticleType");
			else
				af=null;
			request.setAttribute("articleTypeList", am.getAllArticleType());
			
		}else if(action.equals("edit")){
			if (am.updateArticleType(at)) {
				af=new ActionForward("listArticleType");
			} else {
				af=new ActionForward("listArticleType");
			}
		}
		request.setAttribute("msg", am.getMsg());
		return af;
	}
	
}
